Vue.js Quick Start Guide by Ajdin Imsirovic
Author:Ajdin Imsirovic
Language: eng
Format: epub
Tags: COM051260 - COMPUTERS / Programming Languages / JavaScript, COM060080 - COMPUTERS / Web / General, COM060160 - COMPUTERS / Web / Web Programming
Publisher: Packt Publishing
Published: 2018-10-25T09:52:22+00:00
Chaining filters in Vue
The requirements for our app have been updated, and now we need to show some additional, nicely formatted data on the screen.
Since the requirements have changed, we also need to update the data.
The code for this section is available at this pen: https://codepen.io/AjdinImsirovic/pen/BOOazy.
This is the updated JavaScript. To begin, we'll add the el and data options:
new Vue({
el: "#app",
data() {
return {
firstName: "JANE",
lastName: "DOE",
yearOfStudy: 1,
points: 84.44,
additionalPoints: 8
}
},
Still in JS, we'll add the filters:
filters: {
pointsRoundedUp(points){
return Math.ceil(parseFloat(points));
},
pointsToGrade(points){
if(points>90) {
return "A"
}
else if(points>80 && points<=90) {
return "B"
}
else if(points>70 && points<=80) {
return "C"
}
else if(points>60 && points<=70) {
return "D"
}
else if(points>50 && points<=60) {
return "E"
}
else {
return "F"
}
},
yearNumberToWord(yearOfStudy){
// freshman 1, sophomore 2, junior 3, senior 4
if(yearOfStudy==1) {
return "freshman"
} else if(yearOfStudy==2){
return "sophomore"
} else if(yearOfStudy==3){
return "junior"
} else if(yearOfStudy==4){
return "senior"
} else {
return "unknown"
}
},
firstAndLastName(firstName, lastName){
return lastName + ", " + firstName
},
toLowerCase(value){
return value.toLowerCase()
},
capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
}
});
The updated HTML looks like this:
<div id="app">
<h1>A simple grade-rounding Vue app</h1>
<p>Points from test: {{ points }}</p>
<p>Rounded points are: {{ points | pointsRoundedUp }}</p>
<p>Student info:
<!--
<p>Name: {{ firstName, lastName | firstAndLastName | toLowerCase | capitalizeFirstLetter}}</p>
-->
<p>
Name:
{{ lastName | toLowerCase | capitalizeFirstLetter }},
{{ firstName | toLowerCase | capitalizeFirstLetter }}
</p>
<p>Year of study: {{ yearOfStudy | yearNumberToWord }}</p>
<p>Final grade: <strong>{{ points | pointsToGrade }}</strong></p>
</div>
With these chained filters, we achieved the correct formatting of the student's name by virtue of taking the data (which appeared in all CAPS) and piping it through two filters: toLowerCase and capitalizeFirstLetter.
We can also see a commented-out paragraph that shows an unsuccessful approach that capitalizes only the first letter of the last name, but not the first letter of the first name. The reason for this is the firstAndLastName filter which, when applied, combines the full name into a single string.
Note that filters are not cached, which means that they will be always run, just like methods.
For more information on filters, refer to the official documentation at https://vuejs.org/v2/guide/filters.html.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
The Mikado Method by Ola Ellnestam Daniel Brolund(27119)
Hello! Python by Anthony Briggs(25972)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(25310)
Kotlin in Action by Dmitry Jemerov(24415)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(23613)
Dependency Injection in .NET by Mark Seemann(23326)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(21963)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(20867)
Grails in Action by Glen Smith Peter Ledbrook(19886)
Adobe Camera Raw For Digital Photographers Only by Rob Sheppard(17081)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(16848)
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(14474)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(12595)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(11876)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(10657)
Hit Refresh by Satya Nadella(9248)
The Kubernetes Operator Framework Book by Michael Dame(8595)
Exploring Deepfakes by Bryan Lyon and Matt Tora(8449)
Robo-Advisor with Python by Aki Ranin(8394)